import Vue from 'vue'
import command from '@/utils/command'
Vue.use(command)
const state = {
  applicationOpts: [{ id: 12, namecode: '扁鹊中医后台管理' }, { id: 11, namecode: '扁鹊中医用户端小程序' }]
}
const mutations = {
  SET_OPTIONS: (state, { key, data }) => {
    state[key] = data
  }
}
const actions = {
  async getOptions ({ commit, state }, { busiName, rela }) {
    let key = `${busiName}_${rela}`
    if (state[key] && state[key].length) {
      return state[key]
    } else {
      state[key] = []
      const res = await command.executeCommand({
        api: '/api/SelectByRela/MetaType',
        params: {
          index: 0,
          pageSize: 0,
          rela
        },
        data: ["id", "namecode", "NodeType", "nodetype"]
      })
      if (res.flag) {
        commit('SET_OPTIONS', { key, data: res.data })
        return res.data
      }
    }
  },
  async getStoreOptions ({ commit, state }, type) {
    if (state.storeOpts && state.storeOpts.length) {
      let data = state.storeOpts
      if (type == 'exclude') {
        data = data.filter(e => e.id != 1)
      }
      return data
    } else {
      state.storeOpts = []
      const res = await command.executeCommand({
        api: '/api/SelectByConds/ServiceUnit',
        params: {
          index: 0,
          pageSize: 0
        },
        data: {
          "CONDS": [],
          "COLUMNS": ["id", "nodetype", "namecode"]
        }
      })
      if (res.flag) {
        let data = res.data
        commit('SET_OPTIONS', { key: 'storeOpts', data: data })
        if (type == 'exclude') {
          data = data.filter(e => e.id != 1)
        }
        return data
      }
    }
  },
  /**
   * 获取应用列表
   */
  async getApplicationOpts ({ commit, state }) {
    if (state.applicationOpts && state.applicationOpts.length) {
      return state.applicationOpts
    } else {
      state.applicationOpts = []
    }
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
